#include "../include/CRenderBackground.h"
#include "../include/CSprite.h"
#include "../include/CSpriteAnimation.h"

#include <SDL/SDL.h>

//**************************
//Description : Constructor
//Parameters : the speed of scrolling
//Note : None
//**************************
CRenderBackground::CRenderBackground(int _iSpeed)
    : m_iYBackground(0),
    m_iScrollSpeed(_iSpeed)
{
    m_Screen = NULL;

    m_vEntitySprites = NULL;

    m_BackgroundAnimation = NULL;
}

//**************************
//Description : Constructor
//Parameters : The screen,
//              the animation of the background
//              and the speed of scrolling
//Note : None
//**************************
CRenderBackground::CRenderBackground(SDL_Surface * _screen, CSpriteAnimation * _BackgroundAnim, int _iSpeed)
    : m_iYBackground(0),
    m_iScrollSpeed(_iSpeed)
{
    m_Screen = _screen;

    m_vEntitySprites = NULL;

    m_BackgroundAnimation = NULL;
    SetSprites(_BackgroundAnim);
}

//**************************
//Description : Copy Constructor
//Parameters : A CRenderBackground
//Note : None
//**************************
CRenderBackground::CRenderBackground(CRenderBackground & _RenderBackground)
    : m_iYBackground(_RenderBackground.m_iYBackground),
    m_iScrollSpeed(_RenderBackground.m_iScrollSpeed)
{
    m_Screen = _RenderBackground.m_Screen;

    m_vEntitySprites = NULL;

    m_BackgroundAnimation = NULL;

    SetSprites(_RenderBackground.m_BackgroundAnimation);
}

//**************************
//Description : Destructor
//Parameters : None
//Note : None
//**************************
CRenderBackground::~CRenderBackground()
{
    if ( m_BackgroundAnimation != NULL )
        delete m_BackgroundAnimation;

    if ( m_vEntitySprites != NULL )
        delete m_vEntitySprites;
}

//**************************
//Description : Set the initial value for the rendering
//Parameters : The screen,
//              the animation of the background
//              and the speed of scrolling
//Return Value : None
//Note : None
//**************************
void CRenderBackground::SetRender(SDL_Surface * _screen, CSpriteAnimation * _BackgroundAnim, int _iSpeed)
{
    m_Screen = _screen;

    m_iScrollSpeed = _iSpeed;

    SetSprites(_BackgroundAnim);
}

//**************************
//Description : Initialise the rendering to its beginning
//Parameters : None
//Return Value : None
//Note : None
//**************************
void CRenderBackground::InitRender()
{
    m_iYBackground = 0;
}

//**************************
//Description : Set the screen
//Parameters : The screen
//Return Value : None
//Note : None
//**************************
void CRenderBackground::SetScreen(SDL_Surface * _screen)
{
    if (m_Screen == NULL)
        m_Screen = _screen;
}

//**************************
//Description : Set the speed of scrolling
//Parameters : The speed
//Return Value : None
//Note : None
//**************************
void CRenderBackground::SetScrollingSpeed(int _iSpeed)
{
    m_iScrollSpeed = _iSpeed;
}

//**************************
//Description : Return the speed of scrolling
//Parameters : None
//Return Value : The speed
//Note : None
//**************************
int CRenderBackground::GetScrollingSpeed() const
{
    return m_iScrollSpeed;
}

//**************************
//Description : Set sprites for the rendering if it's not done
//Parameters : The animation of the background
//Return Value : None
//Note : None
//**************************
void CRenderBackground::SetSprites(CSpriteAnimation * _BackgroundAnimation)
{
    if ( m_vEntitySprites == NULL && m_BackgroundAnimation == NULL )
    {
        //we create the animation
        m_BackgroundAnimation = new CSpriteAnimation(*_BackgroundAnimation);

        //for the background we don't need sprites for shots
        m_vEntitySprites = new CSprite();

        //we create the sprite
        m_vEntitySprites->InitSprite(m_BackgroundAnimation, m_Screen);

        m_vEntitySprites->Set(0, 0);
    }
}

//**************************
//Description : Render the entity and all things that have connections with it (shots of an entity, power up after death of an enemy, ...)
//Parameters : None
//Return Value : None
//Note : None
//**************************
void CRenderBackground::Render()
{
    SDL_Rect AreaDisplayed;
    AreaDisplayed.x = 0;
    AreaDisplayed.w = GAME_WINDOW_WIDTH;

    SDL_Rect Dest;
    Dest.x = GAME_WINDOW_X;
    Dest.y = 0;

    int iHeightSprite = m_vEntitySprites->GetHeight();

    // we first check if the background has scrolled more than it's own
    // width. And if so, then we must draw 2 images on the screen:
    // the end of one scrolling background and the start of another
    if ( m_iYBackground < GAME_WINDOW_HEIGHT )
    {
        AreaDisplayed.y = iHeightSprite - ( GAME_WINDOW_HEIGHT - m_iYBackground );
        AreaDisplayed.h = GAME_WINDOW_HEIGHT - m_iYBackground;

        //we draw the first part at the normal position
        m_vEntitySprites->Draw(&Dest, &AreaDisplayed);

        AreaDisplayed.y = 0;
        AreaDisplayed.h = m_iYBackground;

        Dest.y = GAME_WINDOW_HEIGHT - m_iYBackground;

        //we draw the second part lower in the screen
        m_vEntitySprites->Draw(&Dest, &AreaDisplayed);
    }
    // if not, we drawn only one background
    else
    {
        AreaDisplayed.y = m_iYBackground;
        AreaDisplayed.h = GAME_WINDOW_HEIGHT;

        m_vEntitySprites->Draw(&Dest, &AreaDisplayed);
    }

    //we multiply the speed by -1 because the scrolling is from up to down
    m_iYBackground += (m_iScrollSpeed * (-1) );

    if ( m_iYBackground < 0 )
    {
        m_iYBackground = iHeightSprite - 1;
    }
}

